package com.swt.gap.common.graph;

import com.swt.gap.model.entity.OntologyNodeProperty;
import com.swt.gap.model.vo.ontology.OntologyNodeVO;
import com.swt.gap.service.OntologyNodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 请填写说明
 *
 * @author sx
 * @date 2020/8/12 17:06
 */
@Slf4j
@Component
public class FulltextIndexCommon {

    private final OntologyNodeService ontologyNodeService;
    private final Neo4jTools neo4jTools;

    public FulltextIndexCommon(OntologyNodeService ontologyNodeService,
                               Neo4jTools neo4jTools) {
        this.ontologyNodeService = ontologyNodeService;
        this.neo4jTools = neo4jTools;
    }

    public void createFulltextIndexAll() {
        List<OntologyNodeVO> ontologyNodes = ontologyNodeService.listAllOntologyNodeAndProperty();
        this.createFulltextIndexAll(ontologyNodes);
    }

    public void createFulltextIndexAll(List<OntologyNodeVO> ontologyNodes) {
        if (!CollectionUtils.isEmpty(ontologyNodes)) {
            Set<String> labelsSet = new HashSet<>();
            Set<String> propertiesSet = new HashSet<>();
            ontologyNodes.forEach(ontologyNode -> {
                String[] labels = ontologyNode.getLabels().split(",");
                labelsSet.add(labels[1]);
                List<OntologyNodeProperty> ontologyNodeProperties = ontologyNode.getOntologyNodePropertyList();
                ontologyNodeProperties.forEach(ontologyNodeProperty -> {
                    propertiesSet.add(ontologyNodeProperty.getPropertyKey());
                });
            });

            StringBuilder sbLabels = new StringBuilder();
            sbLabels.append("[");
            labelsSet.forEach(label -> {
                sbLabels.append("\"").append(label).append("\"").append(",");
            });
            sbLabels.append("]");

            StringBuilder sbProperties = new StringBuilder();
            sbProperties.append("[");
            propertiesSet.forEach(prop -> {
                sbProperties.append("\"").append(prop).append("\"").append(",");
            });
            sbProperties.append("]");
            String allLabels = sbLabels.toString().replace(",]", "]");
            String allProperties = sbProperties.toString().replace(",]", "]");

            String cypher = "CALL db.index.fulltext.createNodeIndex(\"all\"," + allLabels + "," +
                    allProperties + ")";
            neo4jTools.executeCypherNoReturn(cypher);
        }
    }


    public void deleteFulltextIndexAll() {
        String cypher = "CALL db.index.fulltext.drop(\"all\")";
        neo4jTools.executeCypherNoReturn(cypher);
    }

    public void updateFulltextIndexAll() {
        try {
            deleteFulltextIndexAll();
        } catch (Exception e) {
            log.error(e.getMessage());

        } finally {
            createFulltextIndexAll();
        }

    }

    public void updateFulltextIndexAll(List<OntologyNodeVO> ontologyNodes) {
        try {
            deleteFulltextIndexAll();
        } catch (Exception e) {
            log.error(e.getMessage());

        } finally {
            createFulltextIndexAll(ontologyNodes);
        }

    }

}
